package com.oc.mba.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.oc.mba.dao.BaseDAOHibernate;
import com.oc.mba.dao.OcPaperQuestionDAO;
import com.oc.mba.entity.OcPaperQuestion;
import com.oc.mba.util.CommLog;

@Repository
public class OcPaperQuestionDAOImpl extends BaseDAOHibernate implements OcPaperQuestionDAO {
	
	private static final CommLog logger = new CommLog(OcPaperQuestionDAOImpl.class);

	@Override
	public void save(OcPaperQuestion paperQuestion) {
		getHibernateTemplate().save(paperQuestion);
		
	}

	@Override
	public OcPaperQuestion findPaperQuestion(Integer paperID, Integer questionID) {
		DetachedCriteria criteria = DetachedCriteria.forClass(OcPaperQuestion.class);
		criteria.createAlias("ocPaper", "paper");
		criteria.add(Restrictions.eq("paper.id", paperID));
		criteria.add(Restrictions.eq("questionId", questionID));
		List<OcPaperQuestion> list = this.getHibernateTemplate().findByCriteria(criteria);
		if (list!=null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public void removePaperQuestion(Integer paperID, Integer questionID) {
		this.getHibernateTemplate().delete(findPaperQuestion(paperID, questionID));
	}
	
}
